<?php
session_start();

// 验证用户是否登录
if (!isset($_SESSION['username'])) {
    header('Location: login.php');
    exit();
}
require_once 'track_online.php';
// 当前登录的用户名
$currentUser = $_SESSION['username'];

// 获取用户称号
$titlesFile = __DIR__ . '/ch.json';
$allTitles = file_exists($titlesFile) ? json_decode(file_get_contents($titlesFile), true) : [];

// 获取用户头像
function getUserAvatar($username) {
    $userDir = __DIR__ . "/user/$username";
    if (!is_dir($userDir)) return null;
    
    $avatarFiles = ['avatar.png', 'avatar.jpg', 'avatar.jpeg', 'avatar.gif'];
    foreach ($avatarFiles as $file) {
        $path = "$userDir/$file";
        if (file_exists($path)) return "user/$username/$file";
    }
    return null;
}

// 获取用户称号
function getUserTitles($username) {
    global $allTitles;
    return $allTitles[$username] ?? [];
}

// 获取所有用户信息
function getAllUsers($userDir = __DIR__ . '/user') {
    global $allTitles;
    
    $users = [];
    if (is_dir($userDir)) {
        foreach (scandir($userDir) as $username) {
            if ($username !== '.' && $username !== '..' && is_dir("$userDir/$username")) {
                $infoFile = "$userDir/$username/info.json";
                $userData = [];
                
                if (file_exists($infoFile)) {
                    $userData = json_decode(file_get_contents($infoFile), true);
                }
                
                $userData['username'] = $username; // 添加用户名
                $userData['avatar'] = getUserAvatar($username); // 添加头像
                $userData['titles'] = $allTitles[$username] ?? []; // 添加称号
                
                $users[] = $userData;
            }
        }
    }
    return $users;
}

// 搜索用户
$searchResults = [];
$searchPerformed = false;
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['q'])) {
    $searchPerformed = true; // 标记已搜索
    $query = trim($_GET['q']);
    if ($query !== '') {
        $allUsers = getAllUsers();
        foreach ($allUsers as $user) {
            // 排除当前用户自己
            if ($user['username'] !== $currentUser && stripos($user['username'], $query) !== false) {
                $searchResults[] = $user;
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户搜索 - JIILCHAT</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
        }
        
        body {
            background-color: #f5f7fa;
            color: #333;
        }
        
        /* 顶部导航栏 */
        .navbar {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            background: #ffffff;
            padding: 12px 20px;
            display: flex;
            align-items: center;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            z-index: 100;
            border-bottom: 1px solid #e0e4e9;
        }
        
        .back-btn {
            background: none;
            border: none;
            color: #000;
            font-size: 20px;
            margin-right: 15px;
            cursor: pointer;
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease;
        }
        
        .back-btn:hover {
            background: #e4e6eb;
        }
        
        .navbar-title {
            font-size: 18px;
            font-weight: 600;
            flex-grow: 1;
            color: #333;
        }
        
        /* 主内容区 */
        .content {
            margin-top: 70px;
            padding: 20px;
        }
        
        /* 搜索框样式 */
        .search-container {
            background: white;
            border-radius: 15px;
            padding: 20px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            margin-bottom: 25px;
        }
        
        .search-form {
            display: flex;
            gap: 10px;
        }
        
        .search-input {
            flex-grow: 1;
            padding: 14px;
            border: 1px solid #e0e4e9;
            border-radius: 12px;
            font-size: 16px;
            transition: all 0.3s ease;
        }
        
        .search-input:focus {
            border-color: #3498db;
            outline: none;
            box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
        }
        
        .search-btn {
            background: #3498db;
            color: white;
            border: none;
            border-radius: 12px;
            padding: 0 25px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .search-btn:hover {
            background: #2980b9;
        }
        
        /* 搜索结果样式 */
        .results-container {
            background: white;
            border-radius: 15px;
            padding: 20px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
        }
        
        .results-title {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 20px;
            padding-bottom: 12px;
            border-bottom: 1px solid #f0f2f5;
            color: #333;
        }
        
        .no-results {
            text-align: center;
            padding: 30px;
            color: #888;
        }
        
        .no-results i {
            font-size: 48px;
            margin-bottom: 15px;
            opacity: 0.5;
        }
        
        .no-results h3 {
            margin-bottom: 10px;
        }
        
        .user-list {
            list-style: none;
            padding: 0;
        }
        
        .user-card {
            display: flex;
            align-items: center;
            padding: 15px;
            border-radius: 12px;
            margin-bottom: 15px;
            transition: all 0.3s ease;
            border: 1px solid #eee;
        }
        
        .user-card:hover {
            background-color: #f8f9fa;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
        }
        
        .user-avatar {
            width: 60px;
            height: 60px;
            border-radius: 50%;
            object-fit: cover;
            margin-right: 15px;
            background: #e0e4e9;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            font-weight: bold;
            color: #7f8c8d;
        }
        
        .user-info {
            flex-grow: 1;
        }
        
        .user-header {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            gap: 8px;
            margin-bottom: 5px;
        }
        
        .username {
            font-weight: 600;
            font-size: 16px;
            color: #333;
        }
        
        .title-tag {
            display: inline-block;
            padding: 3px 10px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 500;
            color: white;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        .user-meta {
            font-size: 14px;
            color: #7f8c8d;
        }
        
        .view-btn {
            background: #3498db;
            color: white;
            border: none;
            border-radius: 10px;
            padding: 8px 16px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
        }
        
        .view-btn:hover {
            background: #2980b9;
            transform: translateY(-2px);
        }
        
        /* 响应式调整 */
        @media (max-width: 768px) {
            .search-form {
                flex-direction: column;
            }
            
            .search-btn {
                padding: 14px;
            }
            
            .user-card {
                flex-direction: column;
                text-align: center;
            }
            
            .user-avatar {
                margin-right: 0;
                margin-bottom: 15px;
            }
            
            .view-btn {
                margin-top: 15px;
            }
        }
    </style>
</head>
<body>
    <!-- 顶部导航栏 -->
    <div class="navbar">
        <button class="back-btn" onclick="history.back()">
            <i class="fas fa-arrow-left"></i>
        </button>
        <div class="navbar-title">用户搜索</div>
    </div>
    
    <div class="content">
        <!-- 搜索框 -->
        <div class="search-container">
            <form class="search-form" method="GET">
                <input type="text" name="q" class="search-input" 
                       placeholder="输入用户名进行搜索..." 
                       value="<?= htmlspecialchars($_GET['q'] ?? '') ?>" 
                       required>
                <button type="submit" class="search-btn">
                    <i class="fas fa-search"></i> 搜索
                </button>
            </form>
        </div>
        
        <!-- 搜索结果 -->
        <div class="results-container">
            <?php if ($searchPerformed): ?>
                <div class="results-title">
                    搜索结果
                    <span style="font-size:14px;color:#7f8c8d;font-weight:normal">
                        (找到 <?= count($searchResults) ?> 个用户)
                    </span>
                </div>
                
                <?php if (!empty($searchResults)): ?>
                    <ul class="user-list">
                        <?php foreach ($searchResults as $user): ?>
                            <li class="user-card">
                                <?php if ($user['avatar']): ?>
                                    <img src="<?= htmlspecialchars($user['avatar']) ?>" 
                                         alt="<?= htmlspecialchars($user['username']) ?>" 
                                         class="user-avatar">
                                <?php else: ?>
                                    <div class="user-avatar">
                                        <?= substr($user['username'], 0, 1) ?>
                                    </div>
                                <?php endif; ?>
                                
                                <div class="user-info">
                                    <div class="user-header">
                                        <div class="username">
                                            <?= htmlspecialchars($user['username']) ?>
                                        </div>
                                        <?php foreach ($user['titles'] as $title): ?>
                                            <span class="title-tag" style="background:<?= $title['color'] ?>">
                                                <?= htmlspecialchars($title['name']) ?>
                                            </span>
                                        <?php endforeach; ?>
                                    </div>
                                    
                                    <?php if (isset($user['bio']) && !empty($user['bio'])): ?>
                                        <div class="user-meta">
                                            <?= htmlspecialchars($user['bio']) ?>
                                        </div>
                                    <?php endif; ?>
                                </div>
                                
                                <a href="userinfo.php?user=<?= urlencode($user['username']) ?>" 
                                   class="view-btn">
                                    查看资料
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php else: ?>
                    <div class="no-results">
                        <i class="fas fa-user-slash"></i>
                        <h3>未找到用户</h3>
                        <p>没有找到匹配的用户名，请尝试其他搜索词</p>
                    </div>
                <?php endif; ?>
            <?php else: ?>
                <div class="no-results">
                    <i class="fas fa-search"></i>
                    <h3>搜索用户</h3>
                    <p>在搜索框中输入用户名查找其他用户</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
    
    <script>
        // 自动聚焦到搜索框
        window.onload = function() {
            const searchInput = document.querySelector('.search-input');
            if (searchInput) {
                searchInput.focus();
            }
        };
    </script>
</body>
</html>